package hulppakket;

import geometry.Primitive;
import geometry.Triangle;

public class Intersection {

	public float alfa, beta, gamma, t;
	public Primitive primitive;
	public Ray ray;

	public Intersection(Primitive primitive, Ray ray, float alfa, float beta,
			float gamma, float t) {
		this.primitive = primitive;
		this.alfa = alfa;
		this.beta = beta;
		this.gamma = gamma;
		this.t = t;
		this.ray = ray;
	}

	public Color3f getColor(Scene scene) {
		if(this.primitive.getTexture() != null){
			return this.primitive.getTexture().getColor(this.getTextureCoordinate());
		}
		return this.primitive.getMaterial().getShadedColor(scene, this);
	}

	public Point3f getHitpoint() {
		Vector3f result = new Vector3f(ray.getOrigin());
		result = result.add(ray.getDirection().scale(t));
		return new Point3f(result);
	}

	public Vector3f getNormal() {
		return this.primitive.getNormal(alfa, beta, gamma);
	}

	public float getT() {
		return this.t;
	}

	public TexCoord2f getTextureCoordinate() {
		float u, v;
		u = alfa*((Triangle)primitive).textureCoordinateA.x + beta*((Triangle)primitive).textureCoordinateB.x + gamma*((Triangle)primitive).textureCoordinateC.x;
		v = alfa*((Triangle)primitive).textureCoordinateA.y + beta*((Triangle)primitive).textureCoordinateB.y + gamma*((Triangle)primitive).textureCoordinateC.y;
		return new TexCoord2f(u, v);
	}
}
